package com.key.puzzlemodel.transverselongitudinal

import android.graphics.drawable.BitmapDrawable
import android.net.Uri
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.key.puzzlemodel.R
import com.key.puzzlemodel.bean.HVPuzzleBean
import com.key.puzzlemodel.databinding.DialogHvPuzzleBinding
import com.key.puzzlemodel.mvvm.BaseMVVMDialogFragment
import com.key.puzzlemodel.util.UriUtil

/**
 *
 * @功能描述:
 * @创建时间:     2022/1/6
 * @创建者:       阳
 * @修改时间:     2022/1/6
 * @修改者:       阳
 */
class HVMVVMDialog @JvmOverloads constructor(
    val bean: HVPuzzleBean,
    private val okListener: DialogClick? = null
) : BaseMVVMDialogFragment<DialogHvPuzzleBinding>(R.layout.dialog_hv_puzzle),
    View.OnClickListener {


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        isCancelable = false
        mVDB.clickListener = this

        UriUtil.getPathForUri(requireActivity(), Uri.parse(bean.imagePath))?.let {
//            mVDB.imgDialogHvView.setImage(it)
        }

        Glide.with(this).load(bean.imagePath).into(mVDB.imgDialogHvView)

//        mVDB.imgDialogHvView.postDelayed({
////            mVDB.imgDialogHvView.cropRectF = RectF(0f, 0.3f, 0.9f, 0.9f)
//            mVDB.imgDialogHvView.cropRectF = bean.rectf
//        }, 1000)
        mVDB.imgDialogHvView.post {
            mVDB.imgDialogHvView.cropRectF = bean.rectf
        }


//        //设置取消按钮的背景色
//        val drawable = ContextCompat.getDrawable(
//            requireActivity(),
//            R.drawable.icon_puzzle_cancel,
//        ) as GradientDrawable
//
//        drawable.setColor(Color.parseColor("#FFFFFFFF"))
//        mVDB.imgDialogHvCancel.background = drawable
//
//
//        //设置确定按钮的背景色
//        val drawable1 = ContextCompat.getDrawable(
//            requireActivity(),
//            R.drawable.icon_puzzle_ok,
//        ) as GradientDrawable
//        drawable1.setColor(Color.parseColor("#FFFFFFFF"))
//        mVDB.imgDialogHvOk.background = drawable1
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.img_dialog_hv_ok -> {
                bean.rectf = mVDB.imgDialogHvView.cropRectF
                okListener?.clickDialogResult(bean)
                dialog?.dismiss()
            }
            R.id.img_dialog_hv_cancel -> { //关闭
                dialog?.dismiss()
            }
            R.id.show_image_btn -> {
                mVDB.partImage.setViewImageBitmap((mVDB.imgDialogHvView.drawable as BitmapDrawable).bitmap)
                mVDB.partImage.setPartRectF(mVDB.imgDialogHvView.cropRectF)
            }
            R.id.result_image_btn -> {
//                val bitmap = mVDB.partImage.getDrawingBitmap(10, 20, 30, 40, 30, 300, 1, mVDB.imgDialogHvView.cropRectF)
//                mVDB.resultImage.setImageBitmap(bitmap)
            }
        }
    }


    interface DialogClick {
        fun clickDialogResult(bean: HVPuzzleBean)
    }


}